package com.bawei.module_play.repo

import android.util.Log
import com.bawei.lib_common.repo.BaseRepositor
import com.bawei.module_play.api.PlayApiServer
import com.bawei.module_play.entriy.AuthorEntriy
import com.bawei.module_play.entriy.CommendEntity
import com.bawei.module_play.entriy.CommentEntriy
import com.bawei.module_play.entriy.CommentEntriyItem
import com.bawei.module_play.entriy.DanmuEntity
import com.bawei.module_play.entriy.MovieVideoEntityItem
import com.bawei.module_play.entriy.SimpleVideoEntity
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody


import javax.inject.Inject

class PlayVideoRepository @Inject constructor(private val apiserver:PlayApiServer):BaseRepositor<PlayApiServer>(){


    suspend fun getAuthorId(userId:String):Flow<AuthorEntriy>{
        return flow {
            val result=apiserver.getAuthorById(userId)
            if(result.code==200){
                emit(result.data)
            }else{
                Log.d("TAG", "getAuthorId: 获取作者信息失败")
            }
        }
    }

    //获取首页推荐视频
    suspend fun getRecommendSimpleVideo(page: Int, pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            val result = apiserver.getRecommendSimpleVideo(page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取首页分类下视频
    suspend fun getSimpleVideoByChannelId(channelId: String,page: Int,pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            val result = apiserver.getSimpleVideoByChannelId(channelId, page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取放映厅推荐视频
    suspend fun getRecommendMultiVideo(page: Int,pageSize: Int): Flow<List<MovieVideoEntityItem>>{
        return flow {
            val result = apiserver.getRecommendMultiVideo(page, pageSize)
            if (result.code == 200){
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }

    //获取放映厅分类下视频
    suspend fun getMultiVideoByTypeId(typeId: Int, page: Int, pageSize: Int): Flow<List<MovieVideoEntityItem>>{
        return flow {
            val result = apiserver.getMultiVideoByTypeId(typeId, page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //获取视频评论
    suspend fun getCommentByUserId(dataType: Int, itemId: String): Flow<List<CommentEntriyItem>>{
        return flow {
            val result = apiserver.getCommentByUserId(dataType, itemId)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    //发送评论
    suspend fun publishComment(body: RequestBody): Flow<CommendEntity?>{
        return flow {
            val result = apiserver.publishComment(body)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(null)
            }
        }
    }

    //发布弹幕
    suspend fun publishDanmuMsg(requestBody: RequestBody): Flow<String>{
        return flow {
            val result = apiserver.publishDanmuMsg(requestBody)
            if (result.code == 200){
                emit(result.data)
            } else {
                emit("")
            }
        }
    }

    //获取视频弹幕
    suspend fun getVideoDanmuMsg(dataType: Int, itemId: String): Flow<List<DanmuEntity>> {
        return flow {
            val result = apiserver.getVideoDanmuMsg(dataType, itemId)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }





}